package com.phynos.web.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.phynos.dal.mapper.UserMapper;
import com.phynos.dal.model.User;
import com.phynos.web.manager.MySessionCache;
import com.phynos.web.manager.json.JsonResult;
import com.phynos.web.manager.json.ResultCodeEnum;
import com.phynos.web.manager.service.IUserService;

@Service
public class UserService extends BaseService implements IUserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private DataSourceTransactionManager transactionManager;

	public User getUser(long id) {
		return userMapper.getUser(id);
	} 

	@Transactional
	public JsonResult addUser(){
		return JsonResult.code(ResultCodeEnum.UNSURPORT);
	}


	public JsonResult deleteUser(int id){
		DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition();
		//开启新事务
		transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
		TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);
		try {
			//执行操作
			
			transactionManager.commit(transStatus);
		} catch (Exception e) {
			transactionManager.rollback(transStatus);
		}
		return JsonResult.code(ResultCodeEnum.UNSURPORT);
	}

	@Override
	public JsonResult doLogin(HttpServletRequest req, String name,
			String password) {
		HttpSession session = req.getSession(true);
		Object obj = session.getAttribute(MySessionCache.KEY_SESSION_LOGIN);
		MySessionCache sc = null;
		if(obj == null){
			sc = new MySessionCache();
			session.setAttribute(MySessionCache.KEY_SESSION_LOGIN, sc);
		} else {
			sc = (MySessionCache)obj;	
		}
		sc.setLogin(0);		
		return JsonResult.code(ResultCodeEnum.OK);
	}

	@Override
	public String findRoles(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> findPermissions(String name) {
		List<String> permissionList = new ArrayList<>();
        permissionList.add("user:add");
        permissionList.add("user:delete");
        if (name.equals("zhou")) {
            permissionList.add("user:query");
        }
		return permissionList;
	}

	@Override
	public User findByUsername(String name) {
		User user = new User();
		user.setId(1);
		user.setName(name);
		user.setPassword("1");
		user.setAge(24);
		return user;
	}

	@Override
	public JsonResult getUserInfo() {
		User user = new User();
		user.setId(1);
		user.setName("1");
		user.setPassword("1");
		user.setAge(24);		
		return JsonResult.data(user);
	}

}
